﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using University.Workshop.Model;
using System.Data;

namespace University.Workshop.DataAccess
{
    public class EmployeeRegister: IDisposable
    {
        const string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=DataBaseHotel;Integrated Security=True";
        SqlConnection conex;
        public bool ExistEmployee(string ci) 
        {
            bool ok;
            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = "SELECT * FROM [DataBaseHotel].[dbo].[Employee] WHERE CI=@CI";
            cmd.Parameters.AddWithValue("@CI", ci);
            conex.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
                ok = true;
            else
                ok = false;
            return ok;
        }
        public void RegisterEmployee(Employee employee) 
        {
            if (employee == null)
                throw new ArgumentNullException("employee");
            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = @"INSERT INTO [DataBaseHotel].[dbo].[Employee]
                                    ([CI]
                                    ,[Name]
                                    ,[Surname]
                                    ,[WorkArea]
                                    ,[Telephone]
                                    ,[Email]
                                    ,[Address])
                                VALUES
                                    (@CI
                                    ,@Name
                                    ,@Surname
                                    ,@WorkArea
                                    ,@Telephone
                                    ,@Email
                                    ,@Address)";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@CI",employee.CI);
            cmd.Parameters.AddWithValue("@Name",employee.NameEmployee);
            cmd.Parameters.AddWithValue("@Surname",employee.Surname);
            cmd.Parameters.AddWithValue("@WorkArea",employee.WorkArea);
            cmd.Parameters.AddWithValue("@Telephone",employee.Telephone);
            cmd.Parameters.AddWithValue("@Email",employee.Email);
            cmd.Parameters.AddWithValue("@Address",employee.Address);
            try
            {
                conex.Open();
                cmd.ExecuteNonQuery();
               
            }
            catch (Exception e)
            {
                throw new ArgumentException("Error creating a user", e.Message);
            }
        }
        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (conex != null)
                {
                    conex.Dispose();
                    conex = null;
                }
            }
        }

        #endregion
    }
}
